<template>
  <div class="supplyDemand">
    <div class="tabs">
      <div class="tab" :class="{ on: activeName === 0 }" @click="checkNav(0)">
        奶山羊供应
        <span v-if="activeName === 0"></span>
      </div>
      <div class="tab" :class="{ on: activeName === 1 }" @click="checkNav(1)">
        奶山羊需求
        <span v-if="activeName === 1"></span>
      </div>
    </div>
    <van-pull-refresh v-if="activeName === 0" v-model="refreshing" @refresh="onRefresh">
      <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="loadList">
        <template v-if="list.length > 0">
          <div class="item" v-for="(item, index) in list" :key="index">
            <img v-if="item.releaseState == 0" src="@/assets/img/shangjia.png" class="type" alt="" />
            <img v-if="item.releaseState == 1" src="@/assets/img/xiajia.png" class="type" alt="" />

            <div class="item_head">
              <img :src="item.uploadImgs[0] | imgPaths" alt="" />
              <div class="h_r">
                <div class="title">{{ item.content }}</div>
                <div class="con">
                  <span class="price"> ¥{{ item.price }} </span>
                  <span> 浏览量：{{ item.numberOfViews }} </span>
                </div>
              </div>
            </div>
            <div class="btns">
              <div class="btn on" v-if="item.releaseState == 1" @click="deleteItem(item)">删除</div>

              <div class="btn on" v-if="item.releaseState == 0" @click="deleteContent(item)">下架</div>
              <div class="btn on" v-if="item.releaseState == 1" @click="deleteContent(item)">上架</div>
            </div>
          </div>
        </template>
        <div v-else align="center">
          <img src="@/assets/img/zanwu.png" style="width: 200px; margin-top: 136px" alt="" />
        </div>
      </van-list>
    </van-pull-refresh>

    <van-pull-refresh v-else v-model="refreshing" @refresh="onRefresh">
      <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="loadList">
        <template v-if="list2.length > 0">
          <div class="item2" v-for="(itm, index) in list2" :key="index">
            <!-- <img src="@/assets/img/shangjia.png" alt="" class="type2" /> -->
            <img v-if="itm.releaseState == 0" src="@/assets/img/shangjia.png" class="type2" alt="" />
            <img v-if="itm.releaseState == 1" src="@/assets/img/xiajia.png" class="type2" alt="" />

            <div class="item2_content">
              <div class="fz15 c3 mb10">{{ itm.content }}</div>
              <div class="df mb10 fz13 c6">
                <div>品种：{{ itm.breedName }}</div>
                <!-- <div>等级：{{ itm.gradeName }}</div> -->
              </div>
              <div class="df mb10 fz13 c6">
                <div v-if="itm.sex == 0">性别：公</div>
                <div v-if="itm.sex == 1">性别：母</div>

                <div>数量（只）：{{ itm.number }}</div>
              </div>
              <div class="c6 mb10 fz13">联系人：{{ itm.contacts }}</div>
              <div class="c6 mb10 fz13">地点：{{ itm.address }}</div>
              <div class="c6 mb10 fz13">求购时间：{{ itm.startDate }} 至 {{ itm.endDate }}</div>
            </div>
            <div class="btns">
              <div class="btn on" @click="edit(itm)">编辑</div>
              <div class="btn on m-l-5" v-if="itm.releaseState == 1" @click="deleteItem(itm)">删除</div>
              <div class="btn on" v-if="itm.releaseState == 0" @click="deleteContent(itm)">下架</div>
              <div class="btn on" v-if="itm.releaseState == 1" @click="deleteContent(itm)">上架</div>
            </div>
          </div>
        </template>
        <div v-else align="center">
          <img src="@/assets/img/zanwu.png" style="width: 200px; margin-top: 136px" alt="" />
        </div>
      </van-list>
    </van-pull-refresh>

    <div style="height: 56px"></div>
    <div @click="releaseSupply()" class="fabu">
      <img src="@/assets/img/fabu.png" alt="" />
      <span>发布{{ activeName === 0 ? '供应' : '需求' }}</span>
    </div>
  </div>
</template>

<script>
import { Toast, PullRefresh, List, Dialog } from 'vant'
import { sheepSupplyList } from '@/api/supply'
import { postHandle } from '@/api'
import { setValue,clearValue } from '@/utils/auth'
import { imgPaths } from '@/filters'
import { getValue } from '../../utils/auth'

export default {
  name: 'SupplyDemand',
  components: {
    [PullRefresh.name]: PullRefresh,
    [List.name]: List,
    [Dialog.name]: Dialog,
    [Toast.name]: Toast
  },
  data() {
    return {
      activeName: 0,
      refreshing: false,
      loading: false,
      finished: false,
      params: {
        page: 1,
        type: 0,
        perPage: 10,
        // shield: 0,
        queryDataType: 0,
        orderBy: 'createTime',
        subjectInfoId: getValue('subjectInfoId')
      },
      list: [],
      list2: []
    }
  },
  created() {
    this.$bridge.register('refresh', data => {
      this.onRefresh()
    })
  },
  mounted() {},
  methods: {
    loadList() {
      this.list = []
      this.list2 = []
      sheepSupplyList(this.params).then(res => {
        if (res.status === 0) {
          if (this.activeName === 1) {
            res.data.rows.map((item, index) => {
              const objs = {
                content: '',
                releaseState: '',
                sex: '',
                breedName: '',
                numberOfViews: '',
                id: '',
                number: '',
                gradeName: '',
                contacts: '',
                telephone: '',
                address: '',
                startDate: '',
                endDate: ''
              }
              for (var key in item) {
                objs.content = item.content
                objs.releaseState = item.releaseState
                objs.sex = item.sex
                objs.id = item.id
                objs.numberOfViews = item.numberOfViews
                objs.breedName = item.breedName
                objs.number = item.number
                objs.gradeName = item.gradeName
                objs.contacts = item.contacts
                objs.telephone = item.telephone
                objs.startDate = item.startDate
                objs.endDate = item.endDate

                objs.address = item.provinceName + item.cityName + item.countyName + item.address
              }
              this.list2.push(objs)
            })
            console.log(this.list2)
          } else {
            res.data.rows.map((item, index) => {
              const objs = {
                content: '',
                releaseState: '',
                price: '',
                uploadImgs: [],
                numberOfViews: '',
                id: ''
              }
              for (var key in item) {
                objs.content = item.content
                objs.releaseState = item.releaseState

                objs.price = item.price
                objs.id = item.id

                objs.numberOfViews = item.numberOfViews

                objs.uploadImgs = item.uploadImgs.split(',')
              }
              this.list.push(objs)
              console.log(this.list)
            })
          }

          this.loading = false
          this.refreshing = false
          this.finished = res.data.rows.length < this.params.perPage
          if (!this.finished) {
            this.params.page += 1
          }
        }
      })
    },
    // 供应
    releaseSupply() {

      if (this.activeName === 1) {
        clearValue('postDemand-id')
        this.$bridge.call('postDemand')
      } else if (this.activeName === 0) {
        // 需求
        this.$bridge.call('releaseSupply')
      }
    },
    edit(item) {
      setValue('postDemand-id', item.id)
      this.$bridge.call('postDemand')
    },
    deleteItem(item) {
      Dialog.confirm({
        title: '删除确认',
        message: '下架后的商品确认删除?',
        confirmButtonColor: '#37b5b5'
      })
        .then(() => {
          // on confirm
          postHandle(`/agri/api/yzbzh/sheepSupplyAndDemand/delete/${item.id}`).then(res => {
            console.log(res)
            if (res.status === 0) {
              this.list = []
              this.loadList()
            } else {
              Toast({
                message: res.msg,
                position: 'bottom'
              })
            }
          })
        })
        .catch(() => {
          // on cancel
        })
    },
    deleteContent(item) {
      if (item.releaseState === 0) {
        Dialog.confirm({
          title: '下架确认',
          message: '下架后商品将不再对外展示?',
          confirmButtonColor: '#37b5b5'
        })
          .then(() => {
            // on confirm
            postHandle(`/agri/api/yzbzh/sheepSupplyAndDemand/updateReleaseState/${item.id}`).then(res => {
              console.log(res)
              if (res.status === 0) {
                this.list = []
                this.loadList()
              } else {
                Toast({
                  message: res.msg,
                  position: 'bottom'
                })
              }
            })
          })
          .catch(() => {
            // on cancel
          })
      } else {
        Dialog.confirm({
          title: '上架确认',
          message: '上架后的商品不可删除?',
          confirmButtonColor: '#37b5b5'
        })
          .then(() => {
            // on confirm
            postHandle(`/agri/api/yzbzh/sheepSupplyAndDemand/updateReleaseState/${item.id}`).then(res => {
              console.log(res)
              if (res.status === 0) {
                this.list = []
                this.loadList()
              } else {
                Toast({
                  message: res.msg,
                  position: 'bottom'
                })
              }
            })
          })
          .catch(() => {
            // on cancel
          })
      }
    },
    // 下拉刷新
    onRefresh() {
      this.params.page = 1
      this.params.type = this.activeName

      this.loadList()
    },
    checkNav(v) {
      this.activeName = v
      this.params.type = this.activeName
      this.loadList()
    }
  }
}
</script>
<style lang='scss' scoped>
.item2 {
  margin-bottom: 10px;
  background-color: #fff;
  padding: 15px 15px 12px 15px;
  position: relative;
  .type2 {
    width: 72px;
    height: 67px;
    position: absolute;
    right: 0;
    top: 0;
  }
  .item2_content {
    border-bottom: 1px solid #eeeeee;
  }
}
.fz15 {
  font-size: 15px;
}
.c3 {
  color: #333333;
}
.fz13 {
  font-size: 13px;
}
.c6 {
  color: #666666;
}
.df {
  display: flex;
  align-items: center;
}
.mb10 {
  margin-bottom: 10px;
}
.fabu {
  width: 100%;
  height: 56px;
  left: 0;
  bottom: 0;
  background-color: #37b5b5;
  position: fixed;
  font-size: 16px;
  color: #ffffff;
  display: flex;
  justify-content: center;
  align-items: center;
  img {
    width: 20px;
    height: 20px;
    margin-right: 10px;
  }
}
.btns {
  padding-top: 11px;
  display: flex;
  justify-content: flex-end;
  .btn:first-child {
    margin-right: 20px;
    background-color: #37b5b5;
    color: #ffffff;
  }

  .btn {
    width: 50px;
    height: 24px;
    text-align: center;
    line-height: 24px;
    border: 1px solid #37b5b5;
    font-size: 13px;
    color: #37b5b5;
    background-color: #fff;
  }
}
.item {
  position: relative;

  background-color: #fff;
  padding: 15px 15px 12px 15px;
  box-sizing: border-box;
  margin-bottom: 10px;
  .type {
    width: 72px;
    height: 67px;
    position: absolute;
    right: 0;
    top: 0;
  }

  .item_head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-bottom: 15px;
    border-bottom: 1px solid #eeeeee;
    img {
      width: 72px;
      height: 72px;
      margin-right: 12px;
    }
    .h_r {
      width: calc(100% - 84px);
      display: flex;
      height: 72px;

      flex-direction: column;
      justify-content: space-between;
      padding: 4px 0;
      box-sizing: border-box;
      .title {
        font-size: 13px;
        text-overflow: -o-ellipsis-lastline;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        line-clamp: 2;
        -webkit-box-orient: vertical;
        color: #333333;
      }
      .con {
        display: flex;
        align-items: center;
        justify-content: space-between;
        .price {
          color: #ff4141;
          font-size: 16px;
        }
        span {
          color: #999999;
          font-size: 12px;
        }
      }
    }
  }
}
.tabs {
  height: 50px;
  display: flex;
  background-color: #fff;
  border-bottom: 1px solid #eeeeee;
  .tab {
    width: 50%;
    height: 100%;
    text-align: center;
    line-height: 50px;
    position: relative;
    font-size: 15px;
    color: #999999;
    span {
      width: 40px;
      height: 3px;
      background-color: #37b5b5;
      position: absolute;
      left: 50%;
      bottom: 0;
      transform: translate(-50%, 0);
    }
  }
  .tab.on {
    color: #333333;
  }
}
.m-l-5 {
  margin-right: 25px;
}

.titlestyle {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>
